package com.o2opay.car.controller;

import com.o2opay.car.common.Constant;
import com.o2opay.car.common.annotation.Authority;
import com.o2opay.car.common.annotation.ControllerLog;
import com.o2opay.car.common.pojo.AjaxResult;
import com.o2opay.car.common.pojo.PageAjax;
import com.o2opay.car.common.support.DataCache;
import com.o2opay.car.common.utils.CookieUtil;
import com.o2opay.car.common.utils.DateUtil;
import com.o2opay.car.model.CarInfo;
import com.o2opay.car.service.CarInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Component @RequestMapping("/bus/car")
public class CarInfoController extends BaseController{

    @Autowired
    CarInfoService carInfoService;
    @Autowired
    private DataCache dataCache;

    @Authority(opCode = "0401", opName = "车辆管理")
    @RequestMapping("mainPage")
    public String mainPage(Map<String, Object> map) {
        return "auth/car/main";
    }

    @ControllerLog("查询车辆列表")
    @RequestMapping("queryPage")
    @ResponseBody
    @Authority(opCode = "0401", opName = "查询车辆信息列表")
    public PageAjax<CarInfo> queryPage(PageAjax<CarInfo> page, CarInfo car) {
        PageAjax<CarInfo> carInfos = carInfoService.queryPage(page,car);
        return carInfos;
    }

    @Authority(opCode = "040102", opName = "添加车辆信息页面")
    @RequestMapping("addPage")
    public String addPage(Map<String, Object> map) {
        return "auth/car/add";
    }

    @ControllerLog("添加用户")
    @RequestMapping("add")
    @ResponseBody
    @Authority(opCode = "040102", opName = "添加车辆信息")
    public AjaxResult add(CarInfo car,HttpServletRequest request) {
        String sessionId = CookieUtil.get(Constant.SESSION_IDENTITY_KEY, request);
        String userName = dataCache.getString(sessionId);
        if(userName == null || "".equals(userName)){
            throw  new RuntimeException("当前用户丢失，请重新登录");
        }
        car.addCarInfo(userName);
        return carInfoService.saveCar(car);
    }
}
